package org.example.controller;

import org.example.model.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 程序员阿乐
 * @description
 * @createDate 2025/3/24 10:27
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @PostMapping("/register")
    public Integer register(@RequestBody User user) {

        user.setId(1L);
        user.setUsername("admin");
        user.setPassword("123456");

        // 参数验证
        if (user == null || user.getUsername() == null || user.getPassword() == null) {
            return -1;
        }
        // 加密密码
        String encrptedPwd = passwordEncoder.encode(user.getPassword());
        user.setPassword(encrptedPwd);
        Integer res = userService.save(user);
        return res;
    }

    @PostMapping("/login")
    public Integer login(@RequestBody User user) {

        user.setId(1L);
        user.setUsername("admin");
        user.setPassword("123456");

        if (user == null || user.getUsername() == null || user.getPassword() == null) {
            return -1;
        }
        return userService.login(user);
    }

}
